/*
 * 作者：Peter Xiang
 * 联系方式：565067150@qq.com
 * 文档: https://github.com/PxGame
 * 创建时间: 2019/10/28 16:40:09
 */

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using ActionSystem.Core;
using ActionSystem.Core;
using System.Reflection;

namespace XMLib.AM
{
    /// <summary>
    /// ActionListView:当前状态的动作列表
    /// </summary>
    [Serializable]
    public class ActionListView : IDataView
    {
        public override string title => "动作列表";

        public override bool useAre => true;

        private Vector2 scrollPos;
        private Dictionary<Type, int> sortValue=new Dictionary<Type, int>();
        protected override void OnGUI(Rect rect)
        {
            List<BaseStateActionConfig> configs = win.currentActions;
            if (null == configs)
            {
                return;
            }

            EditorGUI.BeginChangeCheck();
            var action = win.currentAction;
            //排序并保证顺序
            for(int i=0;i<configs.Count;i++)
            {
                for(int j=i+1;j<configs.Count;j++)
                {
                    if(!sortValue.ContainsKey(configs[i].GetType()))
                    {
                        var v = configs[i].GetType().GetCustomAttribute<ActionSortAttribute>(true);
                        if(v!=null)
                        {
                            sortValue.Add(configs[i].GetType(), v.value);
                        }else
                        {
                            sortValue.Add(configs[i].GetType(), ActionSortAttribute.DEFAULT_VALUE);
                        }
                    }
                    if (!sortValue.ContainsKey(configs[j].GetType()))
                    {
                        var v = configs[j].GetType().GetCustomAttribute<ActionSortAttribute>(true);
                        if (v != null)
                        {
                            sortValue.Add(configs[j].GetType(), v.value);
                        }
                        else
                        {
                            sortValue.Add(configs[j].GetType(), ActionSortAttribute.DEFAULT_VALUE);
                        }
                    }
                    int iV = sortValue[configs[i].GetType()];
                    int jV = sortValue[configs[j].GetType()];
                    if(iV>jV)
                    {
                        //交换
                        var temp = configs[i];
                        configs[i] = configs[j];
                        configs[j] = temp;
                    }
                }
            }
            //更新index
            for(int i=0;i<configs.Count;i++)
            {
                if(configs[i]==action)
                {
                    win.actionSelectIndex = i;
                    break;
                }
            }
            //绘制list
            win.actionSelectIndex = EditorGUILayoutEx.DrawList(configs, win.actionSelectIndex, ref scrollPos, NewAction, ActionEditorUtility.ItemDrawer);
            if (EditorGUI.EndChangeCheck())
            {
                //win.configModification = true;
            }
        }
        /// <summary>
        /// 点击添加时的动作
        /// </summary>
        /// <param name="adder"></param>
        private void NewAction(Action<BaseStateActionConfig> adder)
        {
            //显示ActionConfigAttribute要求的Action列表，并增加点击item时的回调
            SelectListWindow.ShowTypeWithAttr<ActionConfigAttribute>(t =>
            {
                //点击时，使用反射创建
                BaseStateActionConfig obj = Activator.CreateInstance(t) as BaseStateActionConfig;
                adder(obj);
            });
        }

        public override void OnUpdate()
        {
        }

        public override object CopyData()
        {
            return win.currentAction;
        }

        public override void PasteData(object data)
        {
            if (data.GetType().IsDefined(typeof(ActionConfigAttribute), true))
            {
                win.currentActions.Add(data as BaseStateActionConfig);
                win.actionSelectIndex = win.currentActions.Count - 1;
            }
        }
    }
}